<?php
class InfoAttrAction extends CommonAction {

    public function index() {
        $infoAttr = D('InfoAttr');
        //搜索模块
        parent::search(array(
            'name'=>'属性名称',
            'nickname'=>'属性别名',
            'type'=>'属性类型'
        ), array(
            'type'=>array(
                '下拉框类型'=>1,
                '复选框类型'=>2,
                '单选框类型'=>3,
                '文本框类型'=>4
            )
        ));
        if(!isset($_GET['act'])) {
            parent::page($infoAttr->count());
            $allAttr = $infoAttr->field(array('id', 'name', 'nickname', 'type'))
                                ->order('type ASC')
                                ->limit($this->limit)
                                ->select();
        } else if($_GET['act'] == 'search') {
            parent::page($infoAttr->where($this->map)->count());
            $allAttr = $infoAttr->field(array('id', 'name', 'nickname', 'type'))
                                ->where($this->map)
                                ->order('type ASC')
                                ->limit($this->limit)
                                ->select();
        }

        $this->assign('allattr', $allAttr);
        $this->display();
    }

    public function add() {
        if(isset($_POST['send'])) {
            $infoAttr = D('InfoAttr');
            if($infoAttr->create()) {
                $infoAttr->add() ? $this->success('添加属性成功!', __URL__) : $this->error('系统出错，属性添加失败!', 0);
            } else {
                $this->error($infoAttr->getError(), 0);
            }
        }
        $this->display();
    }

    public function update() {
        $id = $this->_get('id');
        if(isset($id)) {
            $infoAttr = D('InfoAttr');
            if(isset($_POST['send'])) {
                if($infoAttr->create()) {
                    $infoAttr->save() ? $this->success('修改属性成功!', __URL__) : $this->error('系统出错，修改属性失败!', 0);
                } else {
                    $this->error($infoAttr->getError());
                }
            }
            if(!$infoAttr->field('id')->where('id='.$id)->find()) $this->error('不存在此数据!', 0);
            $attr = $infoAttr->field(array('id', 'name', 'nickname', 'details', 'type'))->where('id='.$id)->find();
            $this->assign('attr', $attr);
            $this->display();
        }

    }


    public function delete() {
        $id = $this->_get('id');
        if(isset($id)) {
            $infoAttr = D('InfoAttr');
            if(!$infoAttr->where('id='.$id)->getField('id')) $this->error('不存在此数据!', 0);
            $infoAttr->where('id='.$id)->delete() ? $this->success('删除属性成功!', __URL__) : $this->error('系统出错，删除属性失败!', 0);
        }
    }

    public function mulDelete() {
        $infoAttr = D('InfoAttr');
        $ids = implode(',', $_POST['select']);
        if(!$ids) $this->error('请先勾选一个选项!', 0);
        $map['id'] = array('in', $ids);
        $infoAttr->where($map)->delete() ? $this->success('批量删除属性成功!', __URL__) : $this->error('系统出错，批量删除属性失败!', 0);
    }





    public function uniqueName() {
        $infoAttr = D('InfoAttr');
        $name = $this->_post('name');
        if($infoAttr->where("name='$name'")->getField('id')) echo 1;

    }

    public function uniqueNickname() {
        $infoAttr = D('InfoAttr');
        $nickName = $this->_post('nickname');
        if($infoAttr->where("nickname='$nickName'")->getField('id')) echo 1;

    }


}

?>